package com.hao.windowloading.loading;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.hao.windowloading.R;

/**
 * Created by hao on 16/12/7.
 */
public class LoadingUtils {

    private static final int LOAING = 0;
    private static final int NULL_LOADING = 1;
    private static final int FILE_LOADING = 2;
    private static final int NO_LOADING = 3;

    public interface AgainListener {
        void onAgainListener();
    }


    private Activity activity;
    private FrameLayout frameLayout;
    private ViewHolderLoading viewHolderLoading;
    private ViewHolderFile viewHolderFile;
    private ViewHolderNull viewHolderNull;
    private int now_state = -1;
    private AgainListener againListener;
    private int titleHeight = 0;

    public void setAgainListener(AgainListener againListener) {
        this.againListener = againListener;
    }

    public LoadingUtils(Activity activity, int titleHeight) {
        now_state = NO_LOADING;
        this.activity = activity;
        frameLayout = (FrameLayout) activity.getWindow().getDecorView();
        this.titleHeight = titleHeight;
    }

    /**
     * 开启加载状态
     */
    public void startLoading() {
        if (viewHolderLoading == null)
            viewHolderLoading = new ViewHolderLoading(activity);
        removeView();
        addView(viewHolderLoading.getView());
        now_state = LOAING;
    }

    /**
     * 开启加载为null
     */
    public void startNull() {
        if (viewHolderNull == null)
            viewHolderNull = new ViewHolderNull(activity);
        removeView();
        addView(viewHolderNull.getView());
        now_state = NULL_LOADING;
    }

    /**
     * 开启加载失败
     */
    public void startFile() {
        if (viewHolderFile == null)
            viewHolderFile = new ViewHolderFile(activity, againListener);
        removeView();
        addView(viewHolderFile.getView());
        now_state = FILE_LOADING;
    }

    /**
     * 恢复原本
     */
    public void recovery() {
        removeView();
        now_state = NO_LOADING;
    }


    private void addView(View view) {
        frameLayout.addView(view);
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams();
        layoutParams.setMargins(0, titleHeight, 0, 0);
        view.setLayoutParams(layoutParams);
    }


    /**
     * 清除现有的加载图
     */
    private void removeView() {
        switch (now_state) {
            case LOAING:
                frameLayout.removeView(viewHolderLoading.getView());
                break;
            case NULL_LOADING:
                frameLayout.removeView(viewHolderNull.getView());
                break;
            case FILE_LOADING:
                frameLayout.removeView(viewHolderFile.getView());
                break;
            default:
                break;
        }
    }


    class ViewHolderLoading {
        View view;

        ViewHolderLoading(Context context) {
            initView(context);
        }

        void initView(Context context) {
            view = LayoutInflater.from(context).inflate(R.layout.loading, null);
        }

        public View getView() {
            return view;
        }
    }

    class ViewHolderNull {
        View view;

        ViewHolderNull(Context context) {
            initView(context);
        }

        void initView(Context context) {
            view = LayoutInflater.from(context).inflate(R.layout.null_loading, null);
        }

        public View getView() {
            return view;
        }
    }

    class ViewHolderFile {
        View view;

        ViewHolderFile(Context context, AgainListener againListener) {
            initView(context, againListener);
        }

        void initView(Context context, final AgainListener againListener) {
            view = LayoutInflater.from(context).inflate(R.layout.file_loading, null);
            view.findViewById(R.id.again).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (againListener != null) {
                        againListener.onAgainListener();
                        startLoading();
                    }
                }
            });
        }

        public View getView() {
            return view;
        }
    }

    public void onDestroy() {
        frameLayout = null;
        viewHolderFile = null;
        viewHolderNull = null;
        viewHolderLoading = null;
        activity = null;
    }
}
